package com.handinfo.android.ui.window;

import java.io.DataInputStream;
import java.io.IOException;

import android.graphics.Bitmap;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.game.GameMessage;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWListSubItem;
import com.handinfo.android.uicontrols.controls.DWListbox;
import com.handinfo.android.uicontrols.controls.DWTabControl;
import com.handinfo.android.uicontrols.controls.DWTabPage;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;

public class UIRecord implements IUIWindows{

	private boolean flag = false;
	private DWFrame m_record_frame = null;
	private DWTitle m_record_title = null;
	private DWImageBox m_image_title = null;
	private Bitmap m_bm_title = null;
	private DWImageBox m_record_close = null;
	private DWBackground m_record_background = null;
	private Bitmap m_bm_close = null;
	private DWListbox m_record_listBox = null;
	private DWImageBox[] m_imageBox_list;
	private Bitmap m_bm_list = null;
	private DWListSubItem[] m_list_item;
	private DWTextbox[] m_textBox_zhuangtai;
	private DWTextbox[] m_textBox_bianhao;
	private DWTextbox[] m_textBox_shijian;
	private DWTextbox[] m_textBox_jine;
	private DWTextbox[] m_textBox_fangshi;

	//元宝
	private DWTextbox m_text_yuanbao = null;
	private String m_yuanbao_str = null;
	
	private int m_size = 0;
	private String[] m_zhuangtai;
	private String[] m_dingdanhao;
	private String[] m_shijian;
	private String[] m_jine;
	
	
	private void record(){
		m_record_frame = new DWFrame(DWFrame.FRAME_TYPE_FULL, true);
		m_record_frame.setClickClose(false);
		m_record_title = new DWTitle("", m_record_frame);
		m_image_title = new DWImageBox(m_bm_title);
		m_image_title.setNearAnchor(m_record_title, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 0);
		//关闭窗口
		m_record_close = new DWImageBox(m_bm_close);
		m_record_close.setTouchZoomIn(20, 20);
		m_record_close.addListener(m_close);
		m_record_close.setNearAnchor(m_record_title, Tools.RIGHT_TOP,
				Tools.RIGHT_TOP, 0, 0);
		// 背景
		m_record_background = new DWBackground(
				m_record_frame.getShowWidth()-50,m_record_frame.getShowHeight());
		m_record_background.setShow(true);
		m_record_background.setFill(false);
		m_record_background.setNearAnchor(m_record_frame,
				Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 5);
		//listBox
		m_record_listBox = new DWListbox(m_record_background.getShowWidth()-40, m_record_background.getShowHeight()-40);
		m_record_listBox.setLineSpacing(20);
		m_record_listBox.setNearAnchor(m_record_background, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 0);
		
		m_imageBox_list = new DWImageBox[m_size];
		m_list_item = new DWListSubItem[m_size];
		m_textBox_zhuangtai = new DWTextbox[m_size];//充值状态
		m_textBox_bianhao = new DWTextbox[m_size];//充值编号
		m_textBox_shijian = new DWTextbox[m_size];//充值日期
		m_textBox_jine = new DWTextbox[m_size];//充值金额
//		m_textBox_fangshi = new DWTextbox[5];//充值方式
		for(int i=0;i < m_size;i++){
			m_list_item[i] = new DWListSubItem();
			m_textBox_zhuangtai[i] = new DWTextbox("<mark c=FFBB11>充值状态：</mark>"+m_zhuangtai[i]);
			m_textBox_zhuangtai[i].setFont(DWFont.getFont(20));
			m_textBox_bianhao[i] = new DWTextbox("<mark c=FF0000>订单编号：</mark>"+m_dingdanhao[i]);
			m_textBox_bianhao[i].setFont(DWFont.getFont(20));
			m_textBox_shijian[i] = new DWTextbox("<mark c=FF0000>充值时间：</mark>"+m_shijian[i],310);
			m_textBox_shijian[i].setFont(DWFont.getFont(20));
			m_textBox_jine[i] = new DWTextbox("<mark c=FF0000>充值金额：</mark>"+m_jine[i]+"元");
			m_textBox_jine[i].setFont(DWFont.getFont(20));
//			m_textBox_fangshi[i] = new DWTextbox("<mark c=FF0000>充值方式：</mark>"+"移动充值卡");
			m_imageBox_list[i] = new DWImageBox(m_bm_list,(m_record_background.getShowWidth()-40),(m_textBox_zhuangtai[i].getShowHeight()*3+20));
			m_imageBox_list[i].setShowWideHigh(m_record_listBox.getShowWidth(), (m_textBox_zhuangtai[i].getShowHeight()*3+10));
			
			m_textBox_zhuangtai[i].setNearAnchor(m_list_item[i], Tools.LEFT_TOP, Tools.LEFT_TOP, 5, 5);
			m_textBox_bianhao[i].setNearAnchor(m_textBox_zhuangtai[i], Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 0);
			m_textBox_shijian[i].setNearAnchor(m_textBox_bianhao[i], Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 0);
			m_textBox_jine[i].setNearAnchor(m_textBox_shijian[i], Tools.LEFT_TOP, Tools.RIGHT_TOP, 0, 0);
//			m_textBox_fangshi[i].setNearAnchor(m_textBox_jine[i], Tools.LEFT_TOP, Tools.RIGHT_TOP, 0, 0);
			m_imageBox_list[i].setNearAnchor(m_list_item[i], Tools.LEFT_TOP, Tools.LEFT_TOP, 0, 0);
			
			m_list_item[i].addControls(m_imageBox_list[i]);
			m_list_item[i].addControls(m_textBox_zhuangtai[i]);
			m_list_item[i].addControls(m_textBox_bianhao[i]);
			m_list_item[i].addControls(m_textBox_shijian[i]);
			m_list_item[i].addControls(m_textBox_jine[i]);
//			m_list_item[i].addControls(m_textBox_fangshi[i]);
			
			m_record_listBox.addSubItem(m_list_item[i]);
			
			
		}
	
		//元宝
		m_text_yuanbao = new DWTextbox("当前元宝数："+DWGameManager.getInstance().m_role.m_rmb);
		m_text_yuanbao.setShow(false);
		m_text_yuanbao.setNearAnchor(m_record_frame, Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 20, 0);
					
		m_record_frame.addControl(m_record_background);
		m_record_frame.addControl(m_record_title);
		m_record_frame.addControl(m_image_title);
		m_record_frame.addControl(m_record_close);
		m_record_frame.addControl(m_record_listBox);
		m_record_frame.addControl(m_text_yuanbao);//元宝提示
		DWControlsManager.getInstance().addControl(m_record_frame);
	}
		
	
	//关闭窗口监听
	private DWListener m_close = new DWListener() {
		
		public void OnDoubleClick() {
		}
		
		public void OnClick() {
			UIWindows.getInstance().m_record.close((byte)0);
		}
	};
	
	
	public void init() {
		m_bm_close = UIWindows.createImage("/img/newui/fanhuianniu_1.gnp");
		m_bm_list = UIWindows.createImage("/img/newui/chongzhidi_1.gnp");
		m_bm_title = UIWindows.createImage("/img/newui/chongzhijilu.gnp");
//		m_bm_background = UIWindows.createImage("/img/newui/wodeduiwubk_1.gnp");
//		m_bm_background02 = UIWindows.createImage("/img/newui/yaoqianshuxbjk_1.gnp");
//		m_record_checked = UIWindows.createImage("/img/newui/biaoqianye_2.gnp");
//		m_record_nochecked = UIWindows.createImage("/img/newui/biaoqianye_1.gnp");
	}

	public void open(byte index) {
		if(flag == false){
//			DWGameManager.getInstance().getSendMessage().sendYuanbaoConsumeRecord();
			record();
			flag = true;
		}else{
			if(m_record_frame != null){
				DWControlsManager.getInstance().addControl(m_record_frame);
			}else{
				return;
			}
		}
	}

	public void close(byte index) {
		flag = false;
		DWControlsManager.getInstance().removeControl(m_record_frame);		
	}

	public void dispose() {
		
	}

	public void logic() {
		
	}
	/*
	 * 得到充值记录
	 * 
	 */
	
	public void recvGetChongZhiJiLu(DataInputStream dis) throws IOException {
		DWGameManager.getInstance().getSendMessage().unlockNetwork(GameMessage.锁_充值);
		try {
			byte state = dis.readByte();
			if(state == 0){//其他错误
				DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "其他错误");
			}else if(state == 1){//成功
				byte size = dis.readByte();
				m_size = size;
				m_zhuangtai = new String[size];
				m_dingdanhao = new String[size];
				m_shijian = new String[size];
				m_jine = new String[size];
				for(int i = 0;i < size;i++){
					String zhuangtai = dis.readUTF();
					String dingdanhao = dis.readUTF();
					String shijian = dis.readUTF();
					String chongzhijine = dis.readUTF();
					
					m_zhuangtai[i] = zhuangtai;
					m_dingdanhao[i] = dingdanhao;
					m_shijian[i] = shijian;
					m_jine[i] = chongzhijine;				
				}
			UIWindows.getInstance().m_record.open((byte)0);
			}else if(state == 2){//无充值记录
				DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "您没有充值记录");
			}
			
		} catch (Exception e) {
			Tools.debug(e);
		}
	}

}
